#include <stdio.h>
#include <stdlib.h>
#include "Complex.h"

int Create(Complex &C, float Realpart, float  Imagepart) {
	C.Realpart = Realpart;
	C.Imagepart = Imagepart;
	return 0;
}
float GetReal(Complex c) {
	return c.Realpart;
}
float GetImag(Complex c) {
	return c.Imagepart;
}

// +
Complex Add(Complex c1, Complex c2) {
	Complex c3;
	c3.Realpart = c1.Realpart + c2.Realpart;
	c3.Imagepart = c1.Imagepart + c2.Imagepart;
	return c3;
}
// -
Complex Sub(Complex c1, Complex c2) {
	Complex c3;
	c3.Realpart = c1.Realpart - c2.Realpart;
	c3.Imagepart = c1.Imagepart - c2.Imagepart;
	return c3;
}

// *
Complex Mul(Complex c1, Complex c2) {
	Complex c3;
	c3.Realpart = c1.Realpart * c2.Realpart - c1.Imagepart * c2.Imagepart;
	c3.Imagepart = c1.Realpart * c2.Imagepart + c1.Imagepart * c2.Realpart;
	return c3;
}

//  /
Complex Div(Complex c1, Complex c2) {
	float t = c2.Realpart * c2.Realpart + c2.Imagepart * c2.Imagepart;
	if (t) {
		Complex c3;
		float r = (c1.Realpart * c2.Realpart + c1.Imagepart * c2.Imagepart) / t;
		float i = (c1.Imagepart * c2.Realpart - c1.Realpart * c2.Imagepart) / t;
		c3.Realpart = r;
		c3.Imagepart = i;
		return c3;
	} else {
		puts("The Divisor Is Zero !");
		exit(0);
	}
}

void PrintComplex(Complex c) {
	//实部为0
	if (!c.Realpart) {
		//虚部存在
		if (c.Imagepart)
			printf("%.1lfi", c.Imagepart);
		else //虚部为0
			printf("0");
	} else {
		//实部不为0
		if (c.Imagepart) {
			//正负号处理
			if (c.Imagepart > 0)
				printf("%.1lf+%.1lfi", c.Realpart, c.Imagepart);
			else
				printf("%.1lf%.1lfi", c.Realpart, c.Imagepart);
		} else //虚部为0
			printf("%.1lf", c.Realpart);
	}
	printf("\n");
}


